package cn.sc.summer.construction.sysimpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.rabbitmq.client.Channel;
import cn.sc.summer.constant.rabbit.RabbitConstant;
import cn.sc.summer.rabbitmq.enums.RabbitErrorMessageTypeEnum;
import cn.sc.summer.rabbitmq.po.RabbitMessage;
import cn.sc.summer.rabbitmq.service.RabbitConsumerService;
import cn.sc.summer.rabbitmq.util.RabbitUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * 类名：死信队列监听
 *
 * @author a-xin
 * @date 2024/2/5 11:40
 */
@Slf4j
@Service
public class DeadExchangeImpl extends RabbitConsumerService {

    /**
     * 接口: 死信队列监听
     *
     * @param jsonStr 日志信息
     * @param message rabbit消息提
     * @param channel rabbit信道
     * @author a-xin
     * @date 2023/9/23 15:48
     */
    @Override
    @RabbitListener(queues = RabbitConstant.DEAD_QUEUE, ackMode = "MANUAL")
    public void consumer(String jsonStr, Message message, Channel channel) {
        try {
            RabbitMessage<Object> stringRabbitMessage = RabbitUtil.checkMessage(jsonStr, new TypeReference<Object>() {});
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            if (stringRabbitMessage.getFlag()) {
                log.error("==> 接收到死信队列消息: {}", stringRabbitMessage.getMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
            rabbitErrorService.exchangeMessage(e.getMessage(), jsonStr, RabbitErrorMessageTypeEnum.GATEWAY_LOG_WRITE_ERROR);
        }
    }

}
